﻿/**
 * @usage	MovieClip按钮事件简易初始化类
 * <code>
 * mc必须为MovieClip ,不能为SimpleButton
 * ButtonInitialize.btnEffect(mc,mc,1,5);
 * ButtonInitialize.btnClick(mc,onSound);
 * </code>
 */
package org.nickro.project.objectInit{
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.Event;
	
	public class ButtonInitialize {
		/**
		 * @usage	一般用于影片剪辑滑过滑出效果
		 * @param	button		MovieClip点击对象
		 * @param	movieClip	执行效果对象
		 * @param	startFrame	效果开始帧数
		 * @param	endFrame	效果结束帧数
		 * @param	id			效果执行对象的ID值
		 */
		public static function btnEffect(button:MovieClip, movieClip:MovieClip, startFrame:int, endFrame:int) {
			
			button.addEventListener(MouseEvent.MOUSE_OVER,function(){movieClip.onStats = true;});
			button.addEventListener(MouseEvent.MOUSE_OUT,function(){movieClip.onStats = false;});
	
			movieClip.addEventListener(Event.ENTER_FRAME,onEffect);
			
			function onEffect(e:Event):void
			{
				if(e.target.onStats && e.target.currentFrame < endFrame){
					e.target.nextFrame();
				}else if(!e.target.onStats && e.target.currentFrame > startFrame){
					e.target.prevFrame();
				}
			}
		}
		/**
		 * 按钮事件
		 * @param	button			按钮实例
		 * @param	clickHandler	点击处理函数
		 * @param	handlerParams	处理函数参数列表
		 */
		public static function btnClick(button:Sprite, clickHandler:Function,handlerParams:Array = null):void
		{
			var btnObject:* = button;
			btnObject.buttonMode = true;
			btnObject.clickMethod = clickHandler;
			
			btnObject.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void
			{
				if (handlerParams) e.currentTarget.clickMethod.apply(null, [handlerParams]);
				else e.currentTarget.clickMethod.apply(null, null);
			});
		}
	}
}